<?php
/**
 * 固定传播基金项目
 * @author qianxun
 *
 */
class spreadProjectModel extends MY_Model{
    function spreadProjectModel(){
        parent::MY_Model();
        $this->table = 'SpreadProject';
    }
    /**
     * 包装数据
     * {@inheritDoc}
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        //投放类型配置
        $spreadMediaType = $this->config->item('spreadMediaType');
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('spreadProjectAuditRs');
        foreach ($list as $k => $v){
            //投放类型
            $v['mediaTypeStr'] = isset($spreadMediaType[(int)$v['mediaType']]) ? $spreadMediaType[(int)$v['mediaType']] : '';
            if ($v['mediaType'] == -1){
                $v['mediaTypeStr'] .= '-'.$v['mediaTypeAlias'];
            }
            //合同文件数据
            $v['contractFile'] = unserialize($v['contractFile']);
            //发票文件数据
            $v['invoiceFile'] = unserialize($v['invoiceFile']);
            //会签审批表
            $v['auditTable'] = unserialize($v['auditTable']);
            //审核进度
            $v['auditRoleStr'] = isset($roleList[$v['auditRole']]) ? $roleList[$v['auditRole']].'已审核' : '待审核';
            //审核结果
            $v['auditRsStr'] = isset($auditRs[$v['auditRs']]) ? $auditRs[$v['auditRs']] : '';
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 包装双月细项数据到项目中
     * @param array $projectList
     */
    function packingItemList($projectList = array()){
        $projectIds = array();
        foreach ($projectList as $v){
            $projectIds[] = (int)$v['id'];
        }
        $this->CI->load->model('spreadProItemModel');
        $itemList = $this->CI->spreadProItemModel->getNewList(array('projectId'=>array('in', $projectIds)));
        $itemListByProjectId = array();
        foreach ($itemList as $v){
            $projectId = (int)$v['projectId'];
            isset($itemListByProjectId[$projectId]) || $itemListByProjectId[$projectId] = array();
            $itemListByProjectId[$projectId][(int)$v['id']] = $v;
        }
        foreach ($projectList as $k => $v){
            $projectId = (int)$v['id'];
            if(isset($itemListByProjectId[$projectId])){
                $v['itemList'] = $itemListByProjectId[$projectId];
            }else{
                $v['itemList'] = array();
            }
            $projectList[$k] = $v;
        }
        return $projectList;
    }
    /**
     * 初始化每双月项目数据
     * @param number $projectId
     */
    function initItem($projectId){
        $findData = $this->getData($projectId);
        if ($findData){
            $contractStartTime = strtotime($findData['contractStart'].' 00:00:00');
            $contractEndTime = strtotime($findData['contractEnd'].' 23:59:59');
            //合同所跨月份数
            $this->CI->load->model('timeUnitModel');
            $monthCount = $this->CI->timeUnitModel->getStartEndMonths($contractStartTime, $contractEndTime);
            //每月所占申请金额(小数点保持2位就行四舍五入)
            $perMonthApplyMoney = round($findData['applyMoney'] / $monthCount, 2);
            $validTimeUnitList = $this->CI->timeUnitModel->getStartEntTimeUnitList($contractStartTime, $contractEndTime);
            //待插入双月项目列表
            $itemList = array();
            foreach ($validTimeUnitList as $v){
                $itemList[] = array(
                    'storeId' => $findData['storeId'],
                    'projectId' => $findData['id'],
                    'year' => $v['year'],
                    'timeUnit' => $v['id'],
                    'procId' => $findData['procId'],
                    'startTime' => $v['startTime'],
                    'endTime' => $v['endTime'],
                    'monthApplyMoney' => $perMonthApplyMoney * $v['months']
                );
            }
            if($itemList){
                $this->CI->load->model('spreadProItemModel');
                //$this->CI->spreadProItemModel->del(array('storeId'=>$findData['storeId'],'projectId'=>$findData['id'],'procId'=>$findData['procId']),'*');
                foreach ($itemList as $itemData){
                    //查询双月项目条件
                    $where = $itemData;
                    unset($where['startTime'],$where['endTime'], $where['monthApplyMoney']);
                    $findData = $this->CI->spreadProItemModel->getData($where);
                    if ($findData) {
                        $id = $this->CI->spreadProItemModel->edit($where, array('startTime' => $itemData['startTime'],'endTime' => $itemData['endTime'],'monthApplyMoney'=>$itemData['monthApplyMoney']));
                    }else{
                        $id = $this->CI->spreadProItemModel->add(array_merge($where, array('startTime' => $itemData['startTime'],'endTime' => $itemData['endTime'],'monthApplyMoney'=>$itemData['monthApplyMoney'],'voucher'=>serialize(array()),'auditRemark'=>'','createTime'=>time()) ) );
                    }
                    $id && $this->CI->spreadProItemModel->updateMoney($id);
                }
            }
        }
    }
    /**
     * 获取待检核的项目数据列表
     * @param number $procId
     */
    function getWaitSettleAuditList($procId){
        $projectList = $this->getNewList(array('procId'=>$procId));
        $projectList = $this->packingItemList($projectList);
        //载入固定传播基金可检核时段ID集
        $this->CI->load->model('timeUnitModel');
        $startedTimeUnitIds = $this->CI->timeUnitModel->getSpreadStartedTimeUnitIds();
        //过滤掉未开始的双月
        foreach ($projectList as $k => $projectData){
            foreach ($projectData['itemList'] as $itemId => $itemData){
                //未出现的双月过滤
                if (!in_array($itemData['timeUnit'], $startedTimeUnitIds)){
                    unset($projectList[$k]['itemList'][$itemId]);
                }
                //已审核的双月过滤掉
                if (!in_array($itemData['auditRs'], array(-1,0,3))){
                    //unset($projectList[$k]['itemList'][$itemId]);
                }
                
            }
        }
        return $projectList;
    }
    /**
     * 获取项目总金额
     * @param number $storeId
     * @param number $year
     */
    function getTotalMoney($storeId, $year){
        $list = $this->getList(array('storeId'=>$storeId,'year'=>$year));
        $data = array(
            'applyMoney' => 0,//申请总金额
            'auditMoney' => 0,//审核总金额
        );
        foreach ($list as $v){
            $data['applyMoney'] += $v['applyMoney'];
            
            if ($v['auditRole'] == 4 && $v['auditRs'] == 1){
                $data['auditMoney'] += $v['applyMoney'];
            }
        }
        return $data;
    }
    /**
     * 获取项目额度列表
     * @param number $year
     */
    function getTotalMoneyList($year){
        $list = $this->getList(array('storeId !='=>0,'year'=>$year));
        $initData = array(
            'applyMoney' => 0,//申请总金额
            'auditMoney' => 0,//审核总金额
        );
        $moneyList = array();
        foreach ($list as $v){
            $storeId = (int)$v['storeId'];
            isset($moneyList[$storeId]) || $moneyList[$storeId] = $initData;
            $moneyList[$storeId]['applyMoney'] += $v['applyMoney'];
            
            if ($v['auditRole'] == 4 && $v['auditRs'] == 1){
                $moneyList[$storeId]['auditMoney'] += $v['applyMoney'];
            }
        }
        return $moneyList;
    }
}